package com.loong.controller;

import com.google.gson.Gson;
import com.loong.mapper.impl.OrderMapperImpl;
import com.loong.pojo.*;
import com.loong.service.AddressService;
import com.loong.service.CartService;
import com.loong.service.OrderService;
import com.loong.service.impl.AddressServiceImpl;
import com.loong.service.impl.CartServiceImpl;
import com.loong.service.impl.OrderServiceImpl;
import com.loong.utils.Constants;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.util.List;

@WebServlet("/order")
public class OrderController extends BaseServlet{
    public String preView(HttpServletRequest request, HttpServletResponse response){
        String uid = request.getParameter("uid");
        AddressService addressService = new AddressServiceImpl();
        List<Address> addressList = addressService.findAddressByUid(Integer.parseInt(uid));
        CartService cartService = new CartServiceImpl();
        List<Cart> cartList = cartService.findAll(Integer.parseInt(uid));
        request.setAttribute("addressList", addressList);
        request.setAttribute("cartList", cartList);
        return Constants.FORWARD + "/order.jsp";
    }

    public String create(HttpServletRequest request, HttpServletResponse response){
        String uid = request.getParameter("uid");
        String aid = request.getParameter("aid");
        String sum = request.getParameter("sum");

        OrderService orderService = new OrderServiceImpl();
        orderService.createOrder(aid,uid,sum);
        return Constants.FORWARD + "/order?method=show";
    }

    public String show(HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("loginUser");
        if(user == null){
            session.setAttribute("msg","登录后可以查看订单");
            return Constants.REDIRECT + "/login.jsp";
        }
        OrderService orderService = new OrderServiceImpl();
        List<Orders> ordersList = orderService.findOrderByUid(user.getUid());
        request.setAttribute("ordersList", ordersList);
        return Constants.FORWARD + "/orderList.jsp";
    }

    public String detail(HttpServletRequest request, HttpServletResponse response) {
        String  oid = request.getParameter("oid");
        OrderService orderService = new OrderServiceImpl();
        Orders orders = orderService.findOrderByOid(oid);
        request.setAttribute("order", orders);

        return Constants.FORWARD + "/orderDetail.jsp";
    }

    public String success(HttpServletRequest request, HttpServletResponse response){
        String oid = request.getParameter("oid");
        String result = request.getParameter("result");

        Gson gson = new Gson();
        WeiXin weiXin = gson.fromJson(result, WeiXin.class);
        String resultCode = weiXin.getResult().getResult_code();
        if(resultCode!= null && resultCode.equals("SUCCESS")){
            OrderService orderService = new OrderServiceImpl();
            orderService.updateStateByOid(oid);

            return Constants.FORWARD + "/order?method=show";
        }else {
            HttpSession session = request.getSession();
            session.setAttribute("msg","支付失败，请重新支付");
            return Constants.REDIRECT + "/message.jsp";
        }
    }
    public String confirm(HttpServletRequest request, HttpServletResponse response) {
        String  oid = request.getParameter("oid");
        OrderService orderService = new OrderServiceImpl();
       orderService.updateStateToFourByOid(oid);

        return Constants.FORWARD + "/order?method=show";
    }
}
